/**
 *
 */
package mblog.web.controller.api;

import javax.servlet.http.HttpServletRequest;

import mblog.core.data.Post;
import mblog.core.data.PostKeyword;
import mblog.core.persist.service.PostKeywordService;
import mblog.core.persist.service.PostService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import mblog.base.lang.Consts;
import mblog.core.biz.PostBiz;
import mblog.web.controller.BaseController;
import mtons.modules.pojos.Paging;

import java.util.List;

/**
 * @author langhsu
 */
@Controller
@RequestMapping("/api")
public class PostJsonController extends BaseController {
    @Autowired
    private PostBiz postBiz;
    @Autowired
    private PostService postService;
    @Autowired
    private PostKeywordService postKeywordService;

    @RequestMapping("/posts.json")
    @ResponseBody
    public Paging posts(Integer pn, HttpServletRequest request) {
        String order = ServletRequestUtils.getStringParameter(request, "ord", Consts.order.NEWEST);
        int gid = ServletRequestUtils.getIntParameter(request, "gid", 0);
        Paging paging = wrapPage(pn);
        paging = postBiz.paging(paging, gid, order);

        return paging;
    }

    @RequestMapping("/search.json")
    @ResponseBody
    public Paging search(@RequestParam(required = false, defaultValue = "0") Integer pn,
                         @RequestParam(required = false, defaultValue = "3") Integer pageSize,
                         String keyword) {
        Paging page = wrapPage(pn, pageSize);
        try {
            if (StringUtils.isNotEmpty(keyword)) {
                postService.search(page, keyword);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return page;
    }

    @RequestMapping("/medias.json")
    @ResponseBody
    public Paging medias(@RequestParam(required = false, defaultValue = "0") Integer pn,
                         @RequestParam(required = false, defaultValue = "12") Integer pageSize) {
        Paging paging = new Paging(pn, pageSize);
        return postBiz.paging(paging, 0, Consts.order.NEWEST);
    }

    @RequestMapping("/load_post.json")
    @ResponseBody
    public Media loadPost(Long id) {
        return new Media(postBiz.getPost(id));
    }


    @RequestMapping("/featureds.json")
    @ResponseBody
    public List<Post> findFeatureds() {
        return postService.findFeatureds();
    }


    @RequestMapping("/post_keywords.json")
    @ResponseBody
    public List<PostKeyword> postKeywords() {
        return postKeywordService.hotKeywords();
    }


}
